<?php

/**
 * @author thainm
 */
require_once dirname(__FILE__) . '/.././Common/Message.php';
require_once dirname(__FILE__) . '/.././Common/Const.php';
// Util Api Class
class UtilApi 
{

    /**
     * Echoing json response to client
     * @param String $status_code Http response code
     * @param Int    $response    Json response
     */
    public function setOutputApi($status, $code, $msg, $result = null, $total = null)
    {

        require_once dirname(__FILE__) . '/OutputApi.php';
        $output = new OutputApi();
        $output->setResponse($status, $code, $msg, $result, $total);
        self::echoRespnse($status, $output->getRespone());
    }

    /**
     * Echo header
     * @param int   $status_code status code to reponse
     * @param array   $response array to respone
     */
    public function echoRespnse($status_code, $response)
    {

        echo self::setJsonHeader($status_code, $response);
    }

    /**
     * Generating random Unique MD5
     */
    public function generateApiKey()
    {
        return md5(uniqid(rand(), true));
    }

    /**
     *  Generate ticket code
     * @return Generate ticket Code
     */
    public function generateTicketCode()
    {
        return self::randString(24);
    }

    /**
     * Return Random String
     * @param int $len
     * @param string $chars
     * @return string
     */
    public function randString($len, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
    {
        $string = '';
        for ($i = 0; $i < $len; $i++) {
            $pos = rand(0, strlen($chars) - 1);
            $string .= $chars{$pos};
        }
        return $string;
    }

    /**
     * json response encode
     * @param string $status_code
     * @param string $response
     * @return type
     */
    public function setJsonHeader($status_code, $response)
    {
        $app = \Slim\Slim::getInstance();
        // Http response code
        $app->status($status_code);

        // setting response content type to json
        $app->contentType('application/json');
        return json_encode($response);
    }

    /**
     * Verifying required params posted or not
     */
    public function verifyRequiredParams($required_fields)
    {
        
        require_once dirname(__FILE__) . '/Paramcheck.php';
        $paramCheck = new ParamCheck;
        if (!$paramCheck->chkRequiredParams($required_fields)) {
            // Required field(s) are missing or empty
            // echo error json and stop the app
            $app = \Slim\Slim::getInstance();
            UtilApi::echoRespnse(API_STATUS_400, $paramCheck->getRespone());
            $app->stop();
        }
    }

    /**
     * Validating email address
     */
    public function validateEmail($email)
    {
        require_once dirname(__FILE__) . '/Paramcheck.php';
        $app = \Slim\Slim::getInstance();
        $paramcheck = new ParamCheck;
        if (!$paramcheck->chkEmail($email)) {
            UtilApi::echoRespnse(API_STATUS_400, $paramcheck->getRespone());
            $app->stop();
        }
    }

    /**
     * Validate Double type
     * @param  $value
     * @param  $key
     */
    public function validateDouble($value, $key)
    {
        require_once dirname(__FILE__) . '/Paramcheck.php';
        $app = \Slim\Slim::getInstance();
        $paramcheck = new ParamCheck;
        if (!$paramcheck->chkDouble($value, $key)) {
            UtilApi::echoRespnse(API_STATUS_400, $paramcheck->getRespone());
            $app->stop();
        }
    }

    /**
     * Validate Integer Type
     * @param type $value
     * @param type $key
     */
    public function validateInt($value, $key)
    {
        require_once dirname(__FILE__) . '/Paramcheck.php';
        $app = \Slim\Slim::getInstance();
        $paramcheck = new ParamCheck;
        if (!$paramcheck->chkInt($value, $key)) {
            UtilApi::echoRespnse(API_STATUS_400, $paramcheck->getRespone());
            $app->stop();
        }
    }

    /**
     * Validate Integer Type
     * @param type $value
     * @param type $key
     */
    public function validateRequired($value, $key)
    {
        require_once dirname(__FILE__) . '/Paramcheck.php';
        $app = \Slim\Slim::getInstance();
        $paramcheck = new ParamCheck;
        if (!$paramcheck->chkNotEmpty($value, $key)) {
            UtilApi::echoRespnse(API_STATUS_400, $paramcheck->getRespone());
            $app->stop();
        }
    }

    /**
     * 
     * @param string $email
     * @param string $token
     */
    public function sentMailActive($email, $token)
    {
        require_once dirname(__FILE__) . '/SendMail.php';
        $mail = new SendMail;
        $url = URL_VALIDATE . $token;
        $mail->sendMail($email, SUB_TITLE_ACTIVE, self::getMailBody($url));
    }

    /**
     * 
     * @param string $url
     * @return Body Mail
     */
    public function getMailBody($url)
    {
        return
                <<<EOT
        Hello!
<br>
<p>
You are register user at test.net.
<br>
Please click link to <a href="$url" >active</a>
</p>    
EOT;
    }

}
